package com.ruoyi.web.controller.project;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.project.domain.entity.*;
import com.ruoyi.project.domain.query.PhaseDictionaryQuery;
import com.ruoyi.project.domain.query.ProjectCustomQuery;
import com.ruoyi.project.domain.query.ProjectFeeQuery;
import com.ruoyi.project.service.IPhaseDictionaryService;
import com.ruoyi.project.service.IProjectFeeService;
import com.ruoyi.project.service.IProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/project/fee")
public class ProjectFeeController {

    @Resource
    private IProjectFeeService projectFeeService;

    @Resource
    private IProjectService projectService;

    @ApiOperation("分页")
    @GetMapping(value = "/page")
    public R<TableDataInfo> page(ProjectFeeQuery query) {

        return null;
    }


    @ApiOperation("获取往来单位的收付款")
    @GetMapping(value = "/customerFeeList")
    public R customerFeeList(ProjectFeeQuery query) {
        return R.ok(projectFeeService.getReceiveAndPayForCustomer(query));
    }


    @PostMapping(value = "/exportFee")
    public void export(HttpServletResponse response, ProjectFeeQuery query) throws IOException {
        List<ProjectPay> payListlist = projectFeeService.getReceiveAndPayForCustomer(query);
        List<ProjectPayExport> list = new ArrayList<>();

        List<ProjectPay> payList = projectFeeService.getReceiveAndPayForCustomer(query);

        for (ProjectPay pay : payList) {
            ProjectPayExport export = new ProjectPayExport();
            org.springframework.beans.BeanUtils.copyProperties(pay, export);
            list.add(export);
        }

        ExcelUtil<ProjectPayExport> util = new ExcelUtil<>(ProjectPayExport.class);
        util.exportExcel(response, list, "收付款");


    }


    @ApiOperation("所有列表 报销的")
    @GetMapping(value = "/list")
    public R getList(ProjectFeeQuery query) {
        if (StringUtils.isEmpty(query.getProjectNo())) {
            return null;
        }


        Project byId = projectService.getById(query.getProjectNo());
        query.setHousekeeperProject(byId.getHousekeeperProject());
        return R.ok(projectFeeService.getAllFee(query));
    }

    @ApiOperation("所有列表")
    @GetMapping(value = "/listFeeReceive")
    public R listFeeReceive(ProjectFeeQuery query) {
        /**   根据数据观察  vcType(mainType)是36 且 useType是1的表示是报销费用  vcType(mainType)是4 类型是1的为收款
         *  vcType(mainType)是66 类型是1的为付款 vcType(mainType)是93 类型是1的为其他收入*/
        LambdaQueryWrapper<ProjectFee> wrapper = new QueryWrapper<ProjectFee>().lambda()
                .eq(query.getHousekeeperProject() != null, ProjectFee::getHousekeeperProject, query.getHousekeeperProject())
                .eq(query.getUserId() != null, ProjectFee::getUserId, query.getUserId())
                .eq(ProjectFee::getUseType, "1")
                .in(ProjectFee::getMainType, Arrays.asList("4", "66"));//应该把付出去的也查询
        List<ProjectFee> projectFeeList = projectFeeService.list(wrapper);

        return R.ok(projectFeeList);
    }


    @ApiOperation("获取对应的项目报销费用总计")
    @GetMapping(value = "/totalFee")
    public R getProjectFee(ProjectFeeQuery query) {

        //条件 total(报销费用大于0的) 传入用户id 管家婆项目id
        Project byId = projectService.getById(query.getProjectNo());
        query.setHousekeeperProject(byId.getHousekeeperProject());
        List<ProjectFee> allFee = projectFeeService.getAllFee(query);
        BigDecimal total = allFee.stream()
                .map(ProjectFee::getTotal) // 假设 ProjectFee
                .filter(Objects::nonNull) // 过滤掉可能的
                .reduce(BigDecimal.ZERO, BigDecimal::add); // 累加费用
        return R.ok(total);
    }

    @ApiOperation("获取对应的项目报销费用总计")
    @GetMapping(value = "/totalFeeByDate")
    public R totalFeeByDate(ProjectFeeQuery query) {

        //条件 total(报销费用大于0的) 传入用户id 管家婆项目id
        query.setHousekeeperProject(query.getCustomerId());
        List<ProjectFee> allFee = projectFeeService.getAllFeeByDate(query);
        BigDecimal total = allFee.stream()
                .map(ProjectFee::getTotal) // 假设 ProjectFee
                .filter(Objects::nonNull) // 过滤掉可能的
                .reduce(BigDecimal.ZERO, BigDecimal::add); // 累加费用
        return R.ok(total);
    }


    @ApiOperation("获取对应项目的收款信息")
    @GetMapping(value = "/getPay")
    public R getPay(ProjectFeeQuery query) {
        Project byId = projectService.getById(query.getProjectNo());
        query.setHousekeeperProject(byId.getHousekeeperProject());
        List<ProjectFee> allFee = projectFeeService.getAllFee(query);
        BigDecimal total = allFee.stream()
                .map(ProjectFee::getTotal) // 假设 ProjectFee
                .filter(Objects::nonNull) // 过滤掉可能的
                .reduce(BigDecimal.ZERO, BigDecimal::add); // 累加费用
        return R.ok(total);
    }


    @ApiOperation("新增")
    @PostMapping(value = "/save")
    public R savePhaseDictionary(@RequestBody ProjectFee projectFee) {
        return R.ok(projectFeeService.save(projectFee));
    }


    @ApiOperation("修改")
    @PutMapping(value = "/update")
    public R updatePhaseDictionary(@RequestBody ProjectFee projectFee) {
        return R.ok(projectFeeService.updateById(projectFee));
    }


    @ApiOperation("删除")
    @DeleteMapping(value = "/{menuId}")
    public R deletePhaseDictionary(@PathVariable("menuId") Integer id) {
        return R.ok(projectFeeService.removeById(id));
    }


    @ApiOperation("详情")
    @GetMapping(value = "/details")
    public R getDetails(@RequestParam Integer id) {
        ProjectFee projectFee = projectFeeService.getById(id);
        return R.ok(projectFee);
    }


}
